package tq.com.tqteacher.activity.playerView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.githang.statusbar.StatusBarCompat;
import com.xiao.nicevideoplayer.Clarity;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.ArrayList;

import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.TQCropPictureActivity;
import tq.com.tqteacher.view.TQHeader3ViewHold;

public class TQPlayerActivity extends BaseActivity {
    private TQHeader5View mHeader5View;
    static public String PLAY_URL_KEY = "PLAY_URL_KEY";
    private String filePath;
//
//    private IjkVideoView mIjkVideoView;
//    private StandardVideoController mStandardVideoController ;

    private NiceVideoPlayer mNiceVideoPlayer;
    private TxVideoPlayerController mVideoPlayerController;

    static public void showPlayer(String path, Activity base){
        Intent intent = new Intent(base, TQPlayerActivity.class);
        intent.putExtra(PLAY_URL_KEY, path);
        base.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        View view = initNav(R.layout.layout_header_3_view);
//        header3ViewHold = new TQHeader3ViewHold(this, view);
//        header3ViewHold.setTitle("播放");
//        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#FFC11A"), true);
//        header3ViewHold.setBackClick(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                TQPlayerActivity.this.finish();
//            }
//        });

        setStatusBar();
        setContentView(R.layout.activity_player_layout);
        mHeader5View = findViewById(R.id.title);
        mHeader5View.getTitleView().setText("播放");
        mHeader5View.getBackIcon().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TQPlayerActivity.this.finish();
            }
        });

        mNiceVideoPlayer = findViewById(R.id.playerView);
        mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_NATIVE);
        if(mVideoPlayerController == null){
            mVideoPlayerController = new TxVideoPlayerController(this);
            mVideoPlayerController.setTitle("Beautiful China...");
            mVideoPlayerController.setLenght(117000);

            mNiceVideoPlayer.setController(mVideoPlayerController);
        }

//        mIjkVideoView = findViewById(R.id.playerView);
//        if(mStandardVideoController == null){
//            mStandardVideoController = new StandardVideoController(this);
//
//            mIjkVideoView.setVideoController(mStandardVideoController);
//        }
        filePath = getIntent().getStringExtra(PLAY_URL_KEY);


        if(!StringHelper.isEmpty(filePath)){
//            String local = String.format("file://%s", filePath);
//            mIjkVideoView.setUrl(local);
//            mIjkVideoView.start();

//            Clarity lClarity = new Clarity("标清", "270P", filePath);
//            ArrayList<Clarity> lClarities = new ArrayList<>();
//            lClarities.add(lClarity);
//            mVideoPlayerController.setClarity(lClarities, 0);
            mNiceVideoPlayer.setUp(filePath, null);
        }


    }

    @Override
    protected void onStop() {
        super.onStop();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        mIjkVideoView.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mIjkVideoView.resume();
    }

    @Override
    public void onBackPressed() {
//        if (!mIjkVideoView.onBackPressed()) {
//            super.onBackPressed();
//        }

        if (NiceVideoPlayerManager.instance().onBackPressd()) return;
        super.onBackPressed();
    }
}
